/*
 *    Copyright 2009-2023 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       https://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.apache.ibatis.ryan;

import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.ryan.builder.InsertBuilder;

/**
 * 动态SQL提供器
 */
public class SProvider {
  public static final String in = "%s in (%s)";
  public static final String notIn = "%s not in (%s)";

  /**
   * 根据类型自动构建查询语句
   *
   * @param type
   * @param paramName
   * @return
   */
  public String findByColName(Class<?> type, String paramName) {
    SQL sql = new SQL();
    String table = Tools.tableName(type);
    String whereString = String.format(" %s = #{param3}", Tools.toCamelCase(paramName));
    ((SQL)sql.SELECT("*").FROM(table)).WHERE(whereString);
    return sql.toString();
  }

  /**
   * 构建查询语句
   *
   * @param type
   * @param sql
   *
   * @return
   */
  public String select(Class<?> type, String sql) {
    return sql.toString();
  }

  /**
   * 构建插入语句
   *
   * @param <T>
   * @param pojo
   *
   * @return
   */
  public <T> String insert(T pojo) {
    return InsertBuilder.build(pojo).optSql().getSql().toString();
  }

  /**
   * 构建插入语句
   *
   * @param sql
   *
   * @return
   */
  public String insertSql(String sql) {
    return sql.toString();
  }

  public String update(String sql) {
    return sql.toString();
  }

  public String sql(Class<?> type, String sql) {
    return sql.toString();
  }

  /**
   * 执行原生SQL
   *
   * @param sql
   * @param params
   *
   * @return
   */
  public String nativeExecute(String sql, @Param("param3") Map<String, Object> params) {
    return sql.toString();
  }
}
